//
//  FHNetTools.swift
//  WeiBo
//
//  Created by qfh on 16/7/10.
//  Copyright © 2016年 qiufuhe. All rights reserved.
//

import UIKit

import AFNetworking

enum RequestType: Int {
    
    case GET
    case POST
    
    
}

class FHNetTools: AFHTTPSessionManager {
    
    static let shearedNetTools: FHNetTools = {
    
        let tools = FHNetTools()
        
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        
        return tools
        
    
    }()
    
    func request(Type: RequestType, url: String, parameters: AnyObject?, cellBack: ((request: AnyObject?, error: NSError?)->())) -> () {
        
        if Type == .GET {
            
            GET(url, parameters: parameters, progress: nil, success: { (_, response) -> Void in
                

                cellBack(request: response, error: nil)
                
                
                }, failure: { (_, error) -> Void in
                    
                    cellBack(request: nil, error: error)
                    
            })
            
            
            
            
        }else {
            
            
            POST(url, parameters: parameters, progress: nil, success: { (_, response) -> Void in
                
                cellBack(request: response, error: nil)
                
                
                }, failure: { (_, error) -> Void in
                    
                    cellBack(request: nil, error: error)
                    
            })
            
        }
    
    
    
    }
}


extension FHNetTools {

    // 获取access
    func requestAccess_token(code: String, cellBack: ((response: AnyObject?, error: ErrorType?)->())) {
    
        let url = "https://api.weibo.com/oauth2/access_token"
        
        let prame: [String : AnyObject] = [
            "client_id": client_id,
            "client_secret": client_secret,
            "grant_type": "authorization_code",
            "code": code,
            "redirect_uri": redirect_uri
        ]
    
        request(.POST, url: url, parameters: prame, cellBack: cellBack)
    
    }

    /**
     获取用户信息
     */
    func requestUserInfo(userAccount: FHUserAccount, cellBack: ((response: AnyObject?, error: ErrorType?)->())) {
        
        let url = "https://api.weibo.com/2/users/show.json"
        
        let pramems: [String : AnyObject] = [
        
            "access_token": userAccount.access_token!,
            "uid":  "\(userAccount.uid)"
    ]
        
        request(.GET, url: url, parameters: pramems, cellBack: cellBack)
    
    }
    
    



}

/**
*  首页登录
*/


// MARK: - 首页登录

extension FHNetTools {
    
    /**
     获取首页微博数据
     
     - parameter cellBack: <#cellBack description#>
     */
    func requestStatus(max_id: Int64 = 0, since_id: Int64 = 0, cellBack: ((response: AnyObject?, error: ErrorType?)->())) {
    
        let url = "https://api.weibo.com/2/statuses/friends_timeline.json"
        
        let pramt: [String : AnyObject] = [
            
            "access_token": (FHUserAccessViewModel.sharedUserAccessViewModel.userAccess?.access_token!)!,
        
            "since_id": "\(since_id)",
            
            "max_id": "\(max_id)"
            
        ]
        

        self.request(.GET, url: url, parameters: pramt, cellBack: cellBack)

    
    }


}

// MARK: - 发送微博

extension FHNetTools {

    
    func update(status: String, cellBack: ((response: AnyObject?, error: ErrorType?)->())) {
    
        
        let url = "https://api.weibo.com/2/statuses/update.json"
        
        
        let pragm = [
        
            "access_token": (FHUserAccessViewModel.sharedUserAccessViewModel.userAccess?.access_token!)!,
            "status": status
            
        
        ]
        
        request(.POST, url: url, parameters: pragm, cellBack: cellBack)
        
    }
    
    
    func upload(image: UIImage, status: String, cellBack: ((response: AnyObject?, error: ErrorType?)->())) {
    
        let url = "https://upload.api.weibo.com/2/statuses/upload.json"
        
        let pragm = [
            
            "access_token": (FHUserAccessViewModel.sharedUserAccessViewModel.userAccess?.access_token!)!,
            "status": status
            
            
        ]
    
        
        let imageData = UIImageJPEGRepresentation(image, 0.5)!
        
        POST(url, parameters: pragm, constructingBodyWithBlock: { (formData) -> Void in
            
            formData.appendPartWithFileData(imageData, name: "pic", fileName: "test", mimeType: "image/jpeg")
            
            }, progress: nil, success: { (_, response) -> Void in
                cellBack(response: response, error: nil)
            }) { (_, error) -> Void in
                cellBack(response: nil, error: error)
        }
    
    }






}




